﻿using DBUtility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BaseDal
{
    public class AssetUseReturnDal
    {
        // 使用 Lazy<T> 实现单例模式，确保实例的惰性初始化和线程安全。
        private static readonly Lazy<AssetUseReturnDal> _instance = new Lazy<AssetUseReturnDal>(() => new AssetUseReturnDal());

        /// <summary>
        /// 获取类的单例实例。
        /// </summary>
        public static AssetUseReturnDal Instance { get; } = _instance.Value;

        // 私有构造函数，防止外部实例化该类。
        private AssetUseReturnDal()
        { }

        private const string tableH = "AssetUseReturnHead";

        private const string tableB = "AssetUseReturnBody";

        /// <summary>
        /// 查询数据是否存在
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public bool IsExist(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from " + tableH);
            strSql.Append(" where IsDelete=0 " + strWhere);
            return SqlHelper.Exists(strSql.ToString());
        }

        /// <summary>
        /// 查询主表SQL语句
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public StringBuilder GetSelectHeadSql(string strWhere = "")
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("SELECT ID,AUR_Number,AUR_DeptId,AUR_DeptName,AUR_Person,AUR_StartDate,AUR_EndDate,AUR_Reason,AUR_Status,AUR_AssetCount ");
            sb.Append($"FROM {tableH}");
            sb.Append(" Where IsDelete=0 ");

            if (!string.IsNullOrEmpty(strWhere))
            {
                sb.Append(strWhere);
            }

            return sb;
        }

        /// <summary>
        /// 查询子表SQL语句
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public StringBuilder GetSelectBodySql(string strWhere = "")
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("SELECT ID,HeadID,AU_AssetNo,AU_AssetName,AU_Specs,AU_Unit,AU_Price,AU_DeptId,AU_DeptName,AU_Location,StatusTag ");
            sb.Append("FROM " + tableB);
            sb.Append(" Where IsDelete=0 ");

            if (!string.IsNullOrEmpty(strWhere))
            {
                sb.Append(strWhere);
            }

            return sb;
        }

    }
}
